package com.interview.controller;


import com.interview.pojo.Article;
import com.interview.service.ArticleService;
import com.interview.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {


    @Autowired
    private ArticleService articleService;
    //http://localhost:10087/article

    @PostMapping
    public void add(@RequestBody Article article){

        article.setId(Id_Builder.nextId());
        article.setTime(DateUtils.dateStrng());

        articleService.add(article);
    }
    //删除文章
    //http://localhost:10087/article

    @DeleteMapping
    public void del(@RequestBody Article article){
        articleService.del(article);
    }


    //修改文章
    //http://localhost:10087/article/up
    @PostMapping("/up")
    public void pd(@RequestBody Article article){
        articleService.up(article);
    }



    // 推荐文章   ( 就是 随机文章  4篇   (但是要固定的 只能一天变一次)
    //http://localhost:10087/article/queryRecommend

    @GetMapping("/queryRecommend")
    public Result<List<Article>> queryRecommend(){
        List<Article> articles = articleService.queryRecommend();
        return new Result<List<Article>>(true,2000,"查询成功", articles );
    }




 //http://81.70.77.136:10087/article/queryRandom
    // 随机文章  (10篇    查询  随机10篇)
    //http://localhost:10087/article/queryRandom
    @GetMapping("/queryRandom")
    public Result< List<Article>> queryRandom (){
        List<Article> articles = articleService.queryRandom();
        return new Result< List<Article>>(true,2000,"查询成功", articles );
    }


    // 查询全部文章 一次显示10个  而且是倒序  也就是最新文章在第一页
    //http://localhost:10087/article/queryAll

    /**
     *

     * @return
     * @throws Exception=
     */
    @GetMapping("/queryAll")
    public List<Article>  queryAll() throws Exception {

    //查询全部数据
        List<Article> articles = articleService.queryAll();
        return  articles;
    }

    //查询全部数量
    //http://localhost:10087/article/count
    @GetMapping("/count")
    public int count(){
        int i = articleService.query_Count();
        return i;
    }

    //查询全部数量
    //http://localhost:10087/article/count_time?time1=2021-01&&time2=2021-02
    @GetMapping("/count_time")
    public int count_time(String time1,String time2){
        int i = articleService.time_count(time1,time2);
        return i;
    }



    // 查询指定目录下全部文章

    //http://localhost:10087/article/query/1/10?catalogue_id=66-2021jU1zZ7yS9jO0zO6p445E-923
    @GetMapping("/query")
    public List<Article> query(String catalogue_id) throws Exception {
        System.out.println(catalogue_id);
        Article  article =new Article();
        article.setCatalogue_id(catalogue_id);
        List<Article> query1 = articleService.query(article);
        return query1;
    }


    // 查询指定目录下全部文章

    //http://localhost:10087/article/articleId?articleId=129-2021pM1sR9fM17pS24rJ55p423H-887
    @GetMapping("/articleId")
    public Result<Article> queryID(String articleId)  {

        Article  article =new Article();
        article.setId(articleId);
        Article article1 = articleService.queryID(article);
        return new Result< Article>(true,2000,"查询成功",article1 );
    }


    //http://localhost:10087/article/query_Name?head=java
    /**
     * 通过文章名称进行模糊查询

     * @return
     * @throws Exception=
     */
    @GetMapping("/query_Name")
    public List<Article>  query_Name(String head) throws Exception {
        Article article =new Article();
        article.setHead(head);
        List<Article> articles = articleService.queryName(article);
        return  articles;
    }




}
